from cassacoproject.cassaco.models import Category
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

@login_required
def getHTMLSelect(request, id):
    categories = Category.objects.filter(user=request.user)
    output = '<select name="category_id" id="category"><option>--'
    output += '<option value="new">New type...'
    for c in categories:
        selected = ''
        if c.id == int(id):
            selected = 'selected'
        output += '<option %s value="%d">%s' % (selected, c.id, c.description)
    output += '</select>'
    return HttpResponse(output)

@login_required
def new(request):
    cat = Category()
    cat.description = request.POST['description']
    cat.user = request.user
    cat.save()
    return HttpResponse(cat.id)

@login_required
def delete(request, id):
    Category.objects.get(id=int(id), user=request.user).delete()
    return HttpResponseRedirect('/cassaco')